<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>boundedcollectablestorage.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_storage_boundedcollectablestorage.js.html">boundedcollectablestorage.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides a convenient API for data persistence with data
<a name="line17"></a> * expiration and number of items limit.
<a name="line18"></a> *
<a name="line19"></a> * Setting and removing values keeps a max number of items invariant.
<a name="line20"></a> * Collecting values can be user initiated. If oversize, first removes
<a name="line21"></a> * expired items, if still oversize than removes the oldest items until a size
<a name="line22"></a> * constraint is fulfilled.
<a name="line23"></a> *
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.labs.storage.BoundedCollectableStorage&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.array&#39;);
<a name="line29"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line30"></a>goog.require(&#39;goog.iter&#39;);
<a name="line31"></a>goog.require(&#39;goog.storage.CollectableStorage&#39;);
<a name="line32"></a>goog.require(&#39;goog.storage.ErrorCode&#39;);
<a name="line33"></a>goog.require(&#39;goog.storage.ExpiringStorage&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>goog.scope(function() {
<a name="line37"></a>var storage = goog.labs.storage;
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>/**
<a name="line42"></a> * Provides a storage with bounded number of elements, expiring keys and
<a name="line43"></a> * a collection method.
<a name="line44"></a> *
<a name="line45"></a> * @param {!goog.storage.mechanism.IterableMechanism} mechanism The underlying
<a name="line46"></a> *     storage mechanism.
<a name="line47"></a> * @param {number} maxItems Maximum number of items in storage.
<a name="line48"></a> * @constructor
<a name="line49"></a> * @extends {goog.storage.CollectableStorage}
<a name="line50"></a> * @final
<a name="line51"></a> */
<a name="line52"></a>storage.BoundedCollectableStorage = function(mechanism, maxItems) {
<a name="line53"></a>  storage.BoundedCollectableStorage.base(this, &#39;constructor&#39;, mechanism);
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * A maximum number of items that should be stored.
<a name="line57"></a>   * @private {number}
<a name="line58"></a>   */
<a name="line59"></a>  this.maxItems_ = maxItems;
<a name="line60"></a>};
<a name="line61"></a>goog.inherits(storage.BoundedCollectableStorage,
<a name="line62"></a>              goog.storage.CollectableStorage);
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * An item key used to store a list of keys.
<a name="line67"></a> * @const
<a name="line68"></a> * @private
<a name="line69"></a> */
<a name="line70"></a>storage.BoundedCollectableStorage.KEY_LIST_KEY_ = &#39;bounded-collectable-storage&#39;;
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Recreates a list of keys in order of creation.
<a name="line75"></a> *
<a name="line76"></a> * @return {!Array.&lt;string&gt;} a list of unexpired keys.
<a name="line77"></a> * @private
<a name="line78"></a> */
<a name="line79"></a>storage.BoundedCollectableStorage.prototype.rebuildIndex_ = function() {
<a name="line80"></a>  var keys = [];
<a name="line81"></a>  goog.iter.forEach(this.mechanism.__iterator__(true), function(key) {
<a name="line82"></a>    if (storage.BoundedCollectableStorage.KEY_LIST_KEY_ == key) {
<a name="line83"></a>      return;
<a name="line84"></a>    }
<a name="line85"></a>
<a name="line86"></a>    var wrapper;
<a name="line87"></a>    /** @preserveTry */
<a name="line88"></a>    try {
<a name="line89"></a>      wrapper = this.getWrapper(key, true);
<a name="line90"></a>    } catch (ex) {
<a name="line91"></a>      if (ex == goog.storage.ErrorCode.INVALID_VALUE) {
<a name="line92"></a>        // Skip over bad wrappers and continue.
<a name="line93"></a>        return;
<a name="line94"></a>      }
<a name="line95"></a>      // Unknown error, escalate.
<a name="line96"></a>      throw ex;
<a name="line97"></a>    }
<a name="line98"></a>    goog.asserts.assert(wrapper);
<a name="line99"></a>
<a name="line100"></a>    var creationTime = goog.storage.ExpiringStorage.getCreationTime(wrapper);
<a name="line101"></a>    keys.push({key: key, created: creationTime});
<a name="line102"></a>  }, this);
<a name="line103"></a>
<a name="line104"></a>  goog.array.sort(keys, function(a, b) {
<a name="line105"></a>    return a.created - b.created;
<a name="line106"></a>  });
<a name="line107"></a>
<a name="line108"></a>  return goog.array.map(keys, function(v) {
<a name="line109"></a>    return v.key;
<a name="line110"></a>  });
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Gets key list from a local storage. If an item does not exist,
<a name="line116"></a> * may recreate it.
<a name="line117"></a> *
<a name="line118"></a> * @param {boolean} rebuild Whether to rebuild a index if no index item exists.
<a name="line119"></a> * @return {!Array.&lt;string&gt;} a list of keys if index exist, otherwise undefined.
<a name="line120"></a> * @private
<a name="line121"></a> */
<a name="line122"></a>storage.BoundedCollectableStorage.prototype.getKeys_ = function(rebuild) {
<a name="line123"></a>  var keys = storage.BoundedCollectableStorage.superClass_.get.call(this,
<a name="line124"></a>      storage.BoundedCollectableStorage.KEY_LIST_KEY_) || null;
<a name="line125"></a>  if (!keys || !goog.isArray(keys)) {
<a name="line126"></a>    if (rebuild) {
<a name="line127"></a>      keys = this.rebuildIndex_();
<a name="line128"></a>    } else {
<a name="line129"></a>      keys = [];
<a name="line130"></a>    }
<a name="line131"></a>  }
<a name="line132"></a>  return /** @type {!Array.&lt;string&gt;} */ (keys);
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * Saves a list of keys in a local storage.
<a name="line138"></a> *
<a name="line139"></a> * @param {Array.&lt;string&gt;} keys a list of keys to save.
<a name="line140"></a> * @private
<a name="line141"></a> */
<a name="line142"></a>storage.BoundedCollectableStorage.prototype.setKeys_ = function(keys) {
<a name="line143"></a>  storage.BoundedCollectableStorage.superClass_.set.call(this,
<a name="line144"></a>      storage.BoundedCollectableStorage.KEY_LIST_KEY_, keys);
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * Remove subsequence from a sequence.
<a name="line150"></a> *
<a name="line151"></a> * @param {!Array.&lt;string&gt;} keys is a sequence.
<a name="line152"></a> * @param {!Array.&lt;string&gt;} keysToRemove subsequence of keys, the order must
<a name="line153"></a> *     be kept.
<a name="line154"></a> * @return {!Array.&lt;string&gt;} a keys sequence after removing keysToRemove.
<a name="line155"></a> * @private
<a name="line156"></a> */
<a name="line157"></a>storage.BoundedCollectableStorage.removeSubsequence_ =
<a name="line158"></a>    function(keys, keysToRemove) {
<a name="line159"></a>  if (keysToRemove.length == 0) {
<a name="line160"></a>    return goog.array.clone(keys);
<a name="line161"></a>  }
<a name="line162"></a>  var keysToKeep = [];
<a name="line163"></a>  var keysIdx = 0;
<a name="line164"></a>  var keysToRemoveIdx = 0;
<a name="line165"></a>
<a name="line166"></a>  while (keysToRemoveIdx &lt; keysToRemove.length &amp;&amp; keysIdx &lt; keys.length) {
<a name="line167"></a>    var key = keysToRemove[keysToRemoveIdx];
<a name="line168"></a>    while (keysIdx &lt; keys.length &amp;&amp; keys[keysIdx] != key) {
<a name="line169"></a>      keysToKeep.push(keys[keysIdx]);
<a name="line170"></a>      ++keysIdx;
<a name="line171"></a>    }
<a name="line172"></a>    ++keysToRemoveIdx;
<a name="line173"></a>  }
<a name="line174"></a>
<a name="line175"></a>  goog.asserts.assert(keysToRemoveIdx == keysToRemove.length);
<a name="line176"></a>  goog.asserts.assert(keysIdx &lt; keys.length);
<a name="line177"></a>  return goog.array.concat(keysToKeep, goog.array.slice(keys, keysIdx + 1));
<a name="line178"></a>};
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * Keeps the number of items in storage under maxItems. Removes elements in the
<a name="line183"></a> * order of creation.
<a name="line184"></a> *
<a name="line185"></a> * @param {!Array.&lt;string&gt;} keys a list of keys in order of creation.
<a name="line186"></a> * @param {number} maxSize a number of items to keep.
<a name="line187"></a> * @return {!Array.&lt;string&gt;} keys left after removing oversize data.
<a name="line188"></a> * @private
<a name="line189"></a> */
<a name="line190"></a>storage.BoundedCollectableStorage.prototype.collectOversize_ =
<a name="line191"></a>    function(keys, maxSize) {
<a name="line192"></a>  if (keys.length &lt;= maxSize) {
<a name="line193"></a>    return goog.array.clone(keys);
<a name="line194"></a>  }
<a name="line195"></a>  var keysToRemove = goog.array.slice(keys, 0, keys.length - maxSize);
<a name="line196"></a>  goog.array.forEach(keysToRemove, function(key) {
<a name="line197"></a>    storage.BoundedCollectableStorage.superClass_.remove.call(this, key);
<a name="line198"></a>  }, this);
<a name="line199"></a>  return storage.BoundedCollectableStorage.removeSubsequence_(
<a name="line200"></a>      keys, keysToRemove);
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * Cleans up the storage by removing expired keys.
<a name="line206"></a> *
<a name="line207"></a> * @param {boolean=} opt_strict Also remove invalid keys.
<a name="line208"></a> * @override
<a name="line209"></a> */
<a name="line210"></a>storage.BoundedCollectableStorage.prototype.collect =
<a name="line211"></a>    function(opt_strict) {
<a name="line212"></a>  var keys = this.getKeys_(true);
<a name="line213"></a>  var keysToRemove = this.collectInternal(keys, opt_strict);
<a name="line214"></a>  keys = storage.BoundedCollectableStorage.removeSubsequence_(
<a name="line215"></a>      keys, keysToRemove);
<a name="line216"></a>  this.setKeys_(keys);
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/**
<a name="line221"></a> * Ensures that we keep only maxItems number of items in a local storage.
<a name="line222"></a> * @param {boolean=} opt_skipExpired skip removing expired items first.
<a name="line223"></a> * @param {boolean=} opt_strict Also remove invalid keys.
<a name="line224"></a> */
<a name="line225"></a>storage.BoundedCollectableStorage.prototype.collectOversize =
<a name="line226"></a>    function(opt_skipExpired, opt_strict) {
<a name="line227"></a>  var keys = this.getKeys_(true);
<a name="line228"></a>  if (!opt_skipExpired) {
<a name="line229"></a>    var keysToRemove = this.collectInternal(keys, opt_strict);
<a name="line230"></a>    keys = storage.BoundedCollectableStorage.removeSubsequence_(
<a name="line231"></a>        keys, keysToRemove);
<a name="line232"></a>  }
<a name="line233"></a>  keys = this.collectOversize_(keys, this.maxItems_);
<a name="line234"></a>  this.setKeys_(keys);
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Set an item in the storage.
<a name="line240"></a> *
<a name="line241"></a> * @param {string} key The key to set.
<a name="line242"></a> * @param {*} value The value to serialize to a string and save.
<a name="line243"></a> * @param {number=} opt_expiration The number of miliseconds since epoch
<a name="line244"></a> *     (as in goog.now()) when the value is to expire. If the expiration
<a name="line245"></a> *     time is not provided, the value will persist as long as possible.
<a name="line246"></a> * @override
<a name="line247"></a> */
<a name="line248"></a>storage.BoundedCollectableStorage.prototype.set =
<a name="line249"></a>    function(key, value, opt_expiration) {
<a name="line250"></a>  storage.BoundedCollectableStorage.base(
<a name="line251"></a>      this, &#39;set&#39;, key, value, opt_expiration);
<a name="line252"></a>  var keys = this.getKeys_(true);
<a name="line253"></a>  goog.array.remove(keys, key);
<a name="line254"></a>
<a name="line255"></a>  if (goog.isDef(value)) {
<a name="line256"></a>    keys.push(key);
<a name="line257"></a>    if (keys.length &gt;= this.maxItems_) {
<a name="line258"></a>      var keysToRemove = this.collectInternal(keys);
<a name="line259"></a>      keys = storage.BoundedCollectableStorage.removeSubsequence_(
<a name="line260"></a>          keys, keysToRemove);
<a name="line261"></a>      keys = this.collectOversize_(keys, this.maxItems_);
<a name="line262"></a>    }
<a name="line263"></a>  }
<a name="line264"></a>  this.setKeys_(keys);
<a name="line265"></a>};
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * Remove an item from the data storage.
<a name="line270"></a> *
<a name="line271"></a> * @param {string} key The key to remove.
<a name="line272"></a> * @override
<a name="line273"></a> */
<a name="line274"></a>storage.BoundedCollectableStorage.prototype.remove = function(key) {
<a name="line275"></a>  storage.BoundedCollectableStorage.base(this, &#39;remove&#39;, key);
<a name="line276"></a>
<a name="line277"></a>  var keys = this.getKeys_(false);
<a name="line278"></a>  if (goog.isDef(keys)) {
<a name="line279"></a>    goog.array.remove(keys, key);
<a name="line280"></a>    this.setKeys_(keys);
<a name="line281"></a>  }
<a name="line282"></a>};
<a name="line283"></a>
<a name="line284"></a>});  // goog.scope
</pre>


</body>
</html>
